import api from '../../utils/api';
import wxApi from '../../utils/wxApi';
import { $empty, $toast, http, Validate } from '../../utils/utils.js';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        smsCodeBtn: false,
        smsCodeText: '获取验证码',
        phoneNumber: '',
        smsCode: '',
    },

    /**
     * 手机号码 input
     */
    phoneNumberInput(event) {
        this.setData({
            phoneNumberError: '',
            phoneNumber: $empty(event.detail)
        });
    },

    /**
     * 手机短信验证码 input
     */
    smsCodeInput(event) {
        this.setData({
            smsCodeError: '',
            smsCode: $empty(event.detail)
        });
    },

    /**
     * 手机号/短信码注册登录 form
     */
    phoneFormSubmit(e) {
        const { value } = e.detail
        Validate.check(this, this.phoneValidate, value, (res) => {

            if(!res){ 
                return false
            }
            
            const { phoneNumber, smsCode } = this.data;

            http.post(api.default, {
                data: {
                    action: 'reg_mobileadd',
                    mobile: phoneNumber,
                    code: smsCode,
                }
            }).then((res) => {
                const { data } = res.data;
                app.setLogon(data, 2, () => {
                    //登录后跳转
                    $toast(`注册登录`, {
                        icon: 'success',
                        times: 1000,
                    }).then(() => {
                        wxApi.refreshPage();
                    });
                });
            });
        });
    },

    /**
     * 获取手机验证码
     */
    getSmsCode() {
        const { phoneNumber } = this.data;
        Validate.rules("#phoneNumber", (res) => {
            Validate.check(this, res, { 'phoneNumber': phoneNumber }, (rs, err) => {
                if(!rs){
                    $toast(err[0].msg);
                    return false;
                }
                
                http.post(api.default, {
                    data: {
                        action: 'reg_sms',
                        mobile: phoneNumber,
                    }
                }).then((res) => {
                    var times = 60;
                    var countInter = setInterval(() => {
                        if (times >= 1) {
                            times--;
                            this.setData({ smsCodeTimes: times });
                        } else {
                            /**
                             * 获取过期
                             */
                            this.setData({
                                smsCodeBtn: false,
                                smsCodeText: '重新获取',
                                smsCodeTimes: '',
                            });
                            clearInterval(countInter);
                        }
                    }, 1000);

                    this.setData({
                        smsCodeBtn: true,
                        smsCodeText: ' 秒后重发',
                        smsCodeTimes: 60,
                    });
                    $toast('短信发送成功，请注意查收');
                })
            },false);
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        /**
         * 手机验证数据
         */
        Validate.rules("#phoneNumber,#smsCode", (res) => {
            this.phoneValidate = res;
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
})